<template>
    <div class='approval'>
      <el-card class="box-card" style="margin-top: 110px;">
        <div class="head-title2">
          <div class='title'>
            {{$t('achievements.title1')}}
          </div>
          <div class="info">
            <el-tooltip placement="left">
              <div slot="content">
                {{define}}
              </div>
              <div class="btns"></div>
            </el-tooltip>
          </div>
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <!-- 渠道权限 -->
              <div class="searchItem">
                <span>{{$t('overdueData.channelAuth')}}</span>
                <el-select v-model='listQuery.channel' size='small'>
                    <el-option
                    v-for='item in appChannel'
                    :key='item.channel'
                    :label='item.org_name'
                    :value='item.channel'>
                    </el-option>
                </el-select>
              </div>
              <!-- 时间 -->
              <div class='searchItem searchItem-date'>
                <span>{{$t('approvalDetail.riskTime')}}</span>
                <el-date-picker
                  size='small'
                  v-model="listQuery.startDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.beginDate')">
                </el-date-picker>
                <b>{{$t('approval.go')}}</b>
                <el-date-picker
                size='small'
                  v-model="listQuery.endDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.endDate')">
                </el-date-picker>
              </div>
              <div class='menu-title-btn'>
                  <button type='button' @click='search'>
                      {{$t('btn.query')}}
                  </button>
                  <button type='button' @click="handleDownload" :disabled="expDis" v-if="overdueData_btn_export">
                      {{$t('btn.export')}}
                  </button>
              </div>
            </div>
        </div>
      </el-card>
      <el-card class='box-card' style="margin-top: 10px;margin-bottom: 10px;">
        <line-chart :data-all="dataAll"></line-chart>
      </el-card>
      <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
        <div class='title'>
            {{$t('DailyStatistics.ListDetails')}}
        </div>
        <el-table
            v-loading="listLoading"
            ref='multipleTable'
            :data='tableData'
            tooltip-effect='dark'
            style='width: 100%'
            @selection-change='handleSelectionChange'
            border
            size='mini'>
            <!-- 日期 -->
            <el-table-column
            :label="$t('overdueData.time')"
            header-align='center'
            prop="amountDate"
            width="150"
            align='center'>
            </el-table-column>
            <!-- 逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='overdueRate'
            :label="$t('overdueData.firstOvers')">
            </el-table-column>
            <!-- 首逾率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='dayZeroRate'
            :label="$t('OverdueCustomerList.col6')">
            </el-table-column>
            <!-- 7日逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='sevenRate'
            :label="$t('overdueData.firstOvers7')">
            </el-table-column>
            <!-- 14逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='fourteenRate'
            :label="$t('overdueData.firstOvers14')">
            </el-table-column>
            <!-- 21日逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='twentyOneRate'
            :label="$t('overdueData.firstOvers21')">
            </el-table-column>
            <!-- 30日逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='thirtyRate'
            :label="$t('overdueData.firstOvers30')">
            </el-table-column>
            <!-- 30日+逾期率 -->
            <el-table-column
            header-align='center'
            align='center'
            prop='thirtyAfterRate'
            :label="$t('overdueData.firstOvers31')">
            </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { getChannels } from 'api/customQuery/index'
import { pageData, exportData, lineChart } from 'api/synManager/overdueData'
// import { getRiskOverdueCustomerPage, getRiskOverdueCustomerExport } from 'api/RiskManager/OverdueCustomerList/index'
export default {
  data() {
    return {
      define: '',
      overdueData_btn_export: false,
      totalTrail: '',
      totalPass: '',
      totalPassPercent: '',
      totalOverDualPercent: '',
      listLoading: false,
      total: null,
      expDis: true,
      appChannel: [],
      listQuery: {
        limit: 5,
        page: 1,
        // 开始时间
        startDate: '',
        // 结束时间
        endDate: '',
        // app channel
        channel: ''
      },
      tableData: [],
      multipleSelection: [],
      dataAll: []
    };
  },
  computed: {
    ...mapGetters(['elements', 'overdueCustomerListPage', 'getReportDefine'])
  },
  components: {
    'line-chart': () => import('./components/lineChart')
  },
  created() {
    if (this.overdueCustomerListPage.has(this.$route.path)) {
      this.listQuery = this.overdueCustomerListPage.get(this.$route.path)
    }
    this.getTime();
    getChannels().then(res => {
      if (res && res.status === 200) {
        const data = res.data
        this.appChannel = this.appChannel.concat(data);
        this.listQuery.channel = this.appChannel[0] && this.appChannel[0].channel
        this.getList();
        this.getData();
      }
    })
    this.overdueData_btn_export = this.elements['overdueData:btn_export'];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'OVERDUE_RATE_REPORT') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    getTime() {
      this.listQuery.startDate = this.GetDateStr(-7);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      exportData(this.listQuery)
      .then(res => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [this.$t('overdueData.time'), this.$t('overdueData.firstOvers'), this.$t('OverdueCustomerList.col6'), this.$t('overdueData.firstOvers7'), this.$t('overdueData.firstOvers14'), this.$t('overdueData.firstOvers21'), this.$t('overdueData.firstOvers30'), this.$t('overdueData.firstOvers31')];
          const filterVal = ['amountDate', 'overdueRate', 'dayZeroRate', 'sevenRate', 'fourteenRate', 'twentyOneRate', 'thirtyRate', 'thirtyAfterRate'];
          const list = res.data.rows
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, this.$t('route.overdueData'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getData() {
      lineChart(this.listQuery)
      .then(res => {
        if (res.status === 200) {
          // console.log(res)
          this.dataAll = res.data.rows || []
        }
      })
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveOverdueCustomerListPage', { path: this.$route.path, listQuery: this.listQuery });
      pageData(this.listQuery)
        .then(res => {
          if (res && res.status === 200) {
            const data = res.data;
            const rows = data.rows || [];
            this.expDis = false;
            this.listLoading = false;
            this.total = parseInt(data.total || 0);
            this.tableData = rows;
          }
        })
    },
    // 查询
    search() {
      // 时间比较
      this.listQuery.page = 1;
      this.getList();
      this.getData();
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  margin-left: 20px;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../assets/images/question-mark.svg");
}
</style>